package com.zhang.sort;

import java.util.LinkedList;
import java.util.List;

/**
 * @author ZhangJiXin
 * @Description 桶排序简单实现
 * @date 2021/4/25 17:02
 */
public class BucketSort extends Sort<Double>{
    @Override
    protected void sort() {
        List<Double>[] buckets = new List[array.length];
        for (int i = 0; i < array.length; i++) {
            int index = (int) (array[i] * array.length);
            List<Double> bucket = buckets[index];
            if (bucket == null){
                bucket = new LinkedList<>();
                buckets[index] = bucket;
            }
            bucket.add(array[i]);
        }

        int begin = 0;
        for (int i = 0; i < buckets.length; i++) {
            List<Double> bucket = buckets[i];
            if (bucket == null){
                continue;
            }
            bucket.sort(null);
            for (Double aDouble : bucket) {
                array[begin++] = aDouble;
            }
        }
    }
}
